package lab.cloud.project.share;

import java.util.ArrayList;
import java.util.Date;

public class ClientModel implements ClientModelInterface{
	ArrayList chatObservers = new ArrayList();
	ArrayList fileObservers = new ArrayList();
	ArrayList localObservers = new ArrayList();
	
	String chatText;
	String currentDir;
	String author;
	ArrayList fileInfos;
	FileInfo needUpdate;
	
	ArrayList todayFiles;
	ArrayList weekFiles;
	ArrayList monthFiles;
	
	public ClientModel(){
		fileInfos = new ArrayList();
		chatText = "";
		currentDir = System.getProperty("user.home");
	}
	public ClientModel(String author){
		//fileInfos = getAllFileRMI();
		chatText = "";
		currentDir = System.getProperty("user.home");
		this.author = author;
	}
	public String getAuthor(){
		return author;
	}
	public void setAuthor(String name){
		author = name;
	}
	public void updateFileInfo(){
		//shareFileRMI(needUpdate);
		if (needUpdate != null){
			//shareFileRMI(needUpdate);
		}
	}
	public void setNeedUpdate(FileInfo u){
		needUpdate = u;
	}
	public FileInfo getNeedUpdate(){
		return needUpdate;
	}
	public void notifyChatObservers() {
		for(int i = 0; i < chatObservers.size(); i++) {
			ChatObserver observer = (ChatObserver)chatObservers.get(i);
			observer.updateChat();
		}
	}
	public void notifyFileObservers(){
		for(int i = 0; i < fileObservers.size(); i++){
			FileObserver observer = (FileObserver) fileObservers.get(i);
			observer.updateFile();
		}
	}
	public void notifyLocalObservers(){
		for(int i = 0; i < localObservers.size(); i++){
			LocalObserver observer = (LocalObserver) localObservers.get(i);
			observer.updateLocal();
		}
	}
	@Override
	public void addFileInfo(FileInfo n) {
		fileInfos.add(n);
		notifyFileObservers();
	}
	public FileInfo getFileInfo(String name){
		for (int i = 0; i < fileInfos.size(); i++){
			FileInfo c = (FileInfo) fileInfos.get(i);
			if (name.equals(c.getFileName())){
				return c;
			}
		}
		return null;
	}
	@Override
	public String getChatShowText() {
		return chatText;
	}

	@Override
	public String getCurrentDir() {
		return currentDir;
	}

	@Override
	public void registerChatObserver(ChatObserver o) {
		chatObservers.add(o);
	}

	@Override
	public void registerFileObserver(FileObserver o) {
		fileObservers.add(o);
	}

	@Override
	public void registerLocalObserver(LocalObserver o) {
		localObservers.add(o);
	}

	@Override
	public void removeChatObserver(ChatObserver o) {
		int i = chatObservers.indexOf(o);
		if (i >= 0) {
			chatObservers.remove(i);
		}
	}

	@Override
	public void removeFileInfo(FileInfo d) {
		int i = fileInfos.indexOf(d);
		if (i >= 0){
			fileInfos.remove(i);
		}
	}

	@Override
	public void removeFileObserver(FileObserver o) {
		int i = fileObservers.indexOf(o);
		if (i >= 0) {
			fileObservers.remove(i);
		}
	}

	@Override
	public void removeLocalObserver(LocalObserver o) {
		int i = localObservers.indexOf(o);
		if (i >= 0) {
			localObservers.remove(i);
		}
	}

	@Override
	public void setChatShowText(String text) {
		chatText = chatText + '\n' + text;
		notifyChatObservers();
	}

	@Override
	public void setCurrentDir(String path) {
		currentDir = path;
		notifyLocalObservers();
	}
	@Override
	public ArrayList getFileInfos() {
		return fileInfos;
	}
	public void parseFileByTime(){
		todayFiles = new ArrayList();
		weekFiles = new ArrayList();
		monthFiles = new ArrayList();
		for (int i = 0; i < fileInfos.size(); i++){
			FileInfo c = (FileInfo) fileInfos.get(i);
			Date tomorror = new Date();
			tomorror.setDate(tomorror.getDate() + 1);
			if (c.getFileDate().before(tomorror)){
				todayFiles.add(c);
			}
			Date nextWeek = new Date();
			nextWeek.setDate(nextWeek.getDate() + 7);
			if (c.getFileDate().before(nextWeek)){
				weekFiles.add(c);
			}
			Date nextMonth = new Date();
			nextMonth.setMonth(nextMonth.getMonth() + 1);
			if (c.getFileDate().before(nextMonth)){
				monthFiles.add(c);
			}
		}
	}
	public ArrayList getTodayFiles(){
		return todayFiles;
	}
	public ArrayList getWeekFiles(){
		return weekFiles;
	}
	public ArrayList getMonthFiles(){
		return monthFiles;
	}
}
